//
//  BQHeaderView.m
//  BeeQuick
//
//  Created by 万伶童 on 16/11/19.
//  Copyright © 2016年 six_group. All rights reserved.
//

#import "BQHeaderView.h"
#import "BQLayout.h"
#import "NSAttributedString+CZAdditon.h"
#import "BQHeaderCell.h"
static NSString * cellID = @"cell_id";
@interface BQHeaderView()<UICollectionViewDataSource>
@property(strong,nonatomic)NSMutableArray<UIButton*> * btnArrayM;
@end
@implementation BQHeaderView

-(NSMutableArray<UIButton *> *)btnArrayM
{
    if (_btnArrayM == nil)
    {
        _btnArrayM = [NSMutableArray array];
    }
    return _btnArrayM;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        [self setupUI];
    }
    return self;
}
#pragma 界面搭建
- (void)setupUI
{
    BQLayout * layout = [[BQLayout alloc]init];
    UICollectionView * collectionV = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    
    collectionV.backgroundColor = [UIColor redColor];
    //MARK:代理
    collectionV.dataSource = self;
    
    // MARK:注册
    [collectionV registerClass:[BQHeaderCell class] forCellWithReuseIdentifier:cellID];
    
    [self addSubview:collectionV];
    
    collectionV.showsHorizontalScrollIndicator = NO;
    collectionV.pagingEnabled  = YES;
    collectionV.bounces = NO;
    
    // MARK:创建分页
    UIPageControl * pageCtl = [[UIPageControl alloc]init];
    pageCtl.currentPageIndicatorTintColor = [UIColor blackColor];
    pageCtl.pageIndicatorTintColor = [UIColor whiteColor];
    
    pageCtl.userInteractionEnabled = NO;
    pageCtl.numberOfPages = 4;
    [self addSubview:pageCtl];
    /*
     kButtonPrize = 1,
     kButtonMSha = 2,
     kButtonRedPacket = 3,
     kButtonHive = 4,

     */
    //MARK:设置btn
     [self creatBtnTitle:@"抽奖" ImageName:@"flashBuy副本" tag:kButtonPrize];
    [self creatBtnTitle:@"秒杀" ImageName:@"flashBuy副本" tag:kButtonMSha];
    [self creatBtnTitle:@"红包" ImageName:@"flashBuy副本" tag:kButtonRedPacket];
    [self creatBtnTitle:@"蜂窝" ImageName:@"flashBuy副本" tag:kButtonHive];
    
    
    
    //MARK:设置约束
    [collectionV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(self.bounds.size.width, 120));
    }];
    
    [pageCtl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.mas_equalTo(collectionV.mas_bottom).offset(-25);
    }];
    
    [_btnArrayM mas_distributeViewsAlongAxis:0 withFixedSpacing:0 leadSpacing:0 tailSpacing:0];
    [_btnArrayM mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(collectionV.mas_bottom);
        make.bottom.equalTo(self);
    }];
    
}
#pragma 数据源
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 4;
    
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    BQHeaderCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    cell.backgroundColor = [UIColor greenColor];
    return cell;
}

//MARK:创建btn方法
-(void)creatBtnTitle:(NSString*)title ImageName:(NSString*)name tag:(HomeTopButtonType)tag
{
    UIButton * btn = [[UIButton alloc]init];
    //MARK:第三方 图文混排
    [btn setAttributedTitle:[NSAttributedString cz_imageTextWithImage:[UIImage imageNamed:name] imageWH:30 title:title fontSize:10 titleColor:[UIColor blueColor] spacing:5] forState:UIControlStateNormal];
    
#warning 不设置下面的代码就不显示文字
    //设置按钮的行数为0,让按钮上的文字自动换行
    btn.titleLabel.numberOfLines = 0;
    //设置按钮文字的对齐方式为居中对齐
    btn.titleLabel.textAlignment = NSTextAlignmentCenter;
    //让按钮自适应大小
    [btn sizeToFit];
    
    btn.tag = tag;
    //MARK:btn点击事件
    [btn addTarget:self action:@selector(jump:) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:btn];
    [self.btnArrayM addObject:btn];
    NSLog(@"aaaaaaaa%@",self.btnArrayM);
    
}

//MARK:btn跳转
- (void)jump:(HomeTopButtonType )tag
{
    
    
}

@end
